package A_review.review5_9;

import java.util.HashMap;

public class Class567 {

    public boolean checkInclusion(String s1, String s2) {
        HashMap<Character, Integer> need = new HashMap<>();
        char[] chars = s1.toCharArray();
        for (char  a: chars) {
            need.put(a,need.getOrDefault(a,0)+1);
        }
        HashMap<Character, Integer> window = new HashMap<>();
        int left=0;
        int right=0;
        int valid=0;
        int size=s2.length();
        int needLength = s1.length();
        while (right<size){
            char c = s2.charAt(right);
            right++;
            if (need.containsKey(c)){
                window.put(c,window.getOrDefault(c,0)+1);
                if (window.get(c).equals(need.get(c))){
                    valid++;
                }
            }
            int needSize = need.size();
            while (valid==needSize){
                if (right-left==needLength){
                    return true;
                }
                char c1 = s2.charAt(left);
                left++;
                if (need.containsKey(c1)){
                    if (window.get(c1).equals(need.get(c1))){
                        valid--;
                    }
                    window.put(c1, window.get(c1)-1);
                }
            }
        }
        return  false;
    }


    public static void main(String[] args) {
        Class567 class567 = new Class567();
        System.out.println(class567.checkInclusion("ab", "eidboaoo"));
    }
}
